package com.duang.framework.action;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.duang.framework.config.AbstractConfiguration;
import com.duang.framework.config.SystemConfiguration;
import com.duang.framework.render.AbstractRenderFactory;
import com.duang.framework.render.PathViewRender;
import com.duang.framework.render.UrlParseUtils;

/**
 * @QQ 1003031335
 * @author Administrator
 * 
 */
abstract public class BaseServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected final String ERROR_PATH = "/WEB-INF/page/mobile/public/error.jsp";

	@Override
	protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String url = req.getRequestURL().toString();
		invokeController(url.substring(url.lastIndexOf("/") + 1, url.length()),
				req, resp);
	}

	protected boolean isStrLengthLessThanSpecfiedNumber(String str, int number) {
		if (str.length() < number) {
			return true;
		}
		return false;
	}

	protected void addRequestParameterToAttribute(HttpServletRequest req,
			String... names) {
		for (String name : names) {
			req.setAttribute(name, req.getParameter(name));
		}
	}

	protected boolean patternMatcher(String patternStr, String content) {
		Pattern pattern = Pattern.compile(patternStr);
		return pattern.matcher(content).find();
	}

	protected abstract void invokeController(String methodName,
			HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

	protected void renderView(String path, HttpServletRequest req,
			HttpServletResponse resp, PathViewRender viewRender)
			throws ServletException, IOException {
		viewRender.render(path, req, resp);
	}

	protected void renderView(String name, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		renderView(name, req.getRequestURL().toString(), req, resp,
				(PathViewRender) SystemConfiguration.getAppConfig().getRenderFactory()
						.getViewRender());
	}

	protected void renderJson(String content, HttpServletRequest req,
			HttpServletResponse resp) {
		renderText(content, req, resp);
	}

	protected void renderText(String content, HttpServletRequest req,
			HttpServletResponse resp) {
		AbstractRenderFactory.getTextRender().render(content, req, resp);
	}

	/**
	 * @deprecated
	 * @param content
	 * @param req
	 * @param resp
	 */
	protected void renderViewWithOutputStream(String content,
			HttpServletRequest req, HttpServletResponse resp) {
		// req.setAttribute("content", content);
		// AbstractRenderFactory.getOutputStreamRender().render(null, req,
		// resp);
		renderText(content, req, resp);
	}

	protected void renderView(String name, String path, HttpServletRequest req,
			HttpServletResponse resp, PathViewRender viewRender)
			throws ServletException, IOException {
		if (UrlParseUtils.isMobile(path)) {
			if (SystemConfiguration.isDedug()) {
				System.out.println("path---->" + path);
				System.out.println("mobileUrl----->"
						+ SystemConfiguration.getAppConfig().getMobileUrlMap().get(name));
			}
			renderView(SystemConfiguration.getAppConfig().getMobileUrlMap().get(name), req, resp,
					viewRender);
		} else {
			resp.sendError(404);
		}

	}

	protected void renderError(String message, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("errorMessage", message);
		renderView(AbstractConfiguration.ERROR_VIEW, req, resp);
	}

}
